/*
 * BrianClarkeCPE301Design5.c
 *
 * Created: 4/16/2014 8:39:07 PM
 *  Author: Iguana
 */ 
#define F_CPU 8000000UL 	// 8 mhz
#include <util/delay.h> 
#include <avr/io.h> 
#include <avr/interrupt.h>

void init()
	{
	DDRB = 0xFF; 				// Port B is the segment selector.
	DDRD = 0x3E; 				// Port D is the digit selector and TX/RX
	PORTD = 0x3E; 			// D5-D2 is the selector, D1 is the transmitter, and D0 is the receiver
	DDRC = 0;                    // PC0 - analog input
	ADCSRA = 0x87;          // ADC enable with a prescaler of 128
	ADMUX = 0xC0;            // 1.1 V reference and ADC0 left justified
	}
	
int DIGITdisplay(int n)
	{
	/// This function is a look-up table on how to determine which LED segs to light. This mapping is from the display's datasheet.
	int DIGIT = 0x00;
	switch(n) 
		{
		case 0: // The LED number to display is 0
		DIGIT= 0x3F; // This value correlates to displaying a 0 on the 7 seg
		break;
		case 1: // The LED number to display is 1
		DIGIT= 0x18; // This value correlates to displaying a 1 on the 7 seg
		break;
		case 2: // The LED number to display is 2
		DIGIT= 0xAD; // This value correlates to displaying a 2 on the 7 seg
		break;
		case 3: // The LED number to display is 3
		DIGIT= 0xBC; // This value correlates to displaying a 3 on the 7 seg
		break;
		case 4: // The LED number to display is 4
		DIGIT= 0x9A; // This value correlates to displaying a 4 on the 7 seg
		break;
		case 5: // The LED number to display is 5
		DIGIT= 0xB6; // This value correlates to displaying a 5 on the 7 seg
		break;
		case 6: // The LED number to display is 6
		DIGIT= 0xB7; // This value correlates to displaying a 6 on the 7 seg
		break;
		case 7: // The LED number to display is 7
		DIGIT= 0x1C; // This value correlates to displaying a 7 on the 7 seg
		break;
		case 8: // The LED number to display is 8
		DIGIT= 0xBF; // This value correlates to displaying a 8 on the 7 seg
		break;
		case 9: // The LED number to display is 9
		DIGIT= 0xBE; // This value correlates to displaying a 9 on the 7 seg
		break;
	}
	return DIGIT; // Returns the sequence corresponding to the number selected
}
	
int main(void)
	{
	int adc_value;								// ADC Value
	int TEMPERATURE;						// Temperature Value
	int digit0, digit1, digit2;					// Segment Values
	int counter;									// Counter
	init();
	while(1)
		{
		ADCSRA|=(1<<ADSC);                                											// Begin conversion
		while ((ADCSRA&(1<<ADIF))==0);                											// Wait for the conversion to finish
		adc_value = ADCL + (ADCH * 256);              											// Merge input into a single integer
		TEMPERATURE = ((((adc_value * 1.1) / 1024) * 1000) * 9) / 5 + 320; 	  	// Get temperature from C to F		
		digit2 = TEMPERATURE/100;																	// Get the tens place
		digit1 = TEMPERATURE/10 - digit2*10;													// Get the ones place
		digit0 = TEMPERATURE - digit2*100 - digit1*10;										// Get the tenths place (Decimal)
		counter = 0;																							// Run this for 250 units of time before next number
		while(counter<=250) 																				// This is the delay
			{
			PORTD = 0x36; 																				// Enable first digit select
			PORTB = DIGITdisplay(digit2); 															// Light the segments for the first digit (2nd from the left)
			_delay_ms(1); 																					// delay for 1ms
			PORTD = 0x2E; 																				// Enable second digit select
			PORTB = DIGITdisplay(digit1);															// Light the segments for the second digit (3nd from the left)
			_delay_ms(2); 																					
			PORTD = 0x1E; 																				// Enable third digit select
			PORTB = DIGITdisplay(digit0); 															// Light the segments for the third digit (4nd from the left)
			_delay_ms(1); 																					
			PORTD = 0x3E; 																				// Dim everything
			counter++;
			}
		}
	return 0;
	}


